# -*- coding: utf-8 -*-

'''类初始化'''
from genericpath import isfile

from program_top.utilities.csv_and_json_serialisation import clean_load_json, touch_empty_file

def try_load_attr_from_kwargs(self,attr_name,kwargs):
	'''从kwargs入参中取得给定某成员的'''
	if kwargs.__contains__(attr_name):
		setattr(self,attr_name,kwargs[attr_name])
		return True
	else:
		print("no parameter as %s"%attr_name)
		return False
	pass


def load_class_shared_config(self):
	'''为一个类的所有实例加载共享的配置文件，只加载一次'''
	
	class_object=self.__class__
	
	try:
		config_filename=self.environment_pack['class_path']['config_file_dir']+'config.json'
	except Exception as current_error:
		config_filename=self.environment_pack['instance_path']['config_file_dir']+'config.json'
		pass
	
	load_config(class_object, config_filename)
	pass

def load_config(instance_or_class, config_filename):
	'''有配置文件则读取，没有则创建空配置文件'''
	if isfile(config_filename):
		instance_or_class.config=clean_load_json(config_filename)
	else:
		touch_empty_file(config_filename)